﻿using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using IterationLife.PlatformBackEnd.AppServices.Dtos.Commons;
using IterationLife.System.Enums;
using IterationLife.System.ItemModels;
using Util.Helpers;

namespace Applications.Presentation.Extensions.Commons
{
    /// <summary>
    /// 附件控件
    /// </summary>
    public class AttachmentControl<T> : IAttachmentControl<T>
    {
        /// <summary>
        /// 初始化附件控件
        /// </summary>
        /// <param name="helper">HtmlHelper</param>
        public AttachmentControl(HtmlHelper<T> helper)
        {
            _helper = helper;
        }

        /// <summary>
        /// HtmlHelper
        /// </summary>
        private readonly HtmlHelper<T> _helper;

        /// <summary>
        /// 单附件
        /// </summary>
        public MvcHtmlString SingleEdit<TProperty>(Expression<Func<T, TProperty>> expression, Resources resources, FileType type = FileType.Picture)
        {
            var dto = (resources ?? new Resources()).ToDto();
            dto.FileType = type;
            dto.LabelName = Lambda.GetName(expression);
            return _helper.Action(
               "SingleEditView",
               "Attachment",
               new
               {
                   area = "Commons",
                   dto = dto
               });
        }

        /// <summary>
        /// 单附件预览
        /// </summary>
        public MvcHtmlString SinglePreview(Resources resources)
        {
            var dto = (resources ?? new Resources()).ToDto();
            return _helper.Action(
               "SinglePreviewView",
               "Attachment",
               new
               {
                   area = "Commons",
                   dto = dto
               });
        }
    }
}
